class EventBus{

  constructor(){
    this.eventList = {};
  }
  on (eventName,onHandle){
    if(!this.eventList[eventName]){
      this.eventList[eventName] = [];
    }
    this.eventList[eventName].push(onHandle);
    return this;
  }
  emit(eventName,...args){
    if(!this.eventList[eventName]){
      return this;
    }

    for(let i = 0 ;i < this.eventList[eventName].length ;i ++){
      this.eventList[eventName][i](...args); //TODO
    }
    return this;

  }
  off(eventName,onHandle){
    if(!this.eventList[eventName]){
      return this;
    }
    if(onHandle){
      // 删除多项内容
      for(let i = 0 ;i < this.eventList[eventName].length ;i++){
        if(this.eventList[eventName][i] === onHandle){
          this.eventList[eventName][i].splice(i,1);

        }
      }
    }else{
      // 清除所有项
      this.eventList[eventName] = null;
    }
    return this;
  }


}


let eventBus = new EventBus();

function printOne(key){
  console.log("打印key::",key);
}
eventBus.on("one",printOne);
eventBus.on('one',printOne);

eventBus.emit('one',{"ding":"ding"});









